﻿using B.S.Shop.Application.Command;
using B.S.Shop.Domain;
using B.S.Shop.ErrorCode;
using B.S.Shop.Infartructrue.Interfaces;
using MediatR;

namespace B.S.Shop.Application.Commandhandler
{
    public class ShopCreateCommandHandler : IRequestHandler<ShopCreateCommand, APIResult<int>>
    {
        public ShopCreateCommandHandler(IBaseRepository<ShopClass> shopRepository)
        {
            ShopRepository = shopRepository;
        }

        public IBaseRepository<ShopClass> ShopRepository { get; }

        public async Task<APIResult<int>> Handle(ShopCreateCommand request, CancellationToken cancellationToken)
        {
            var paths = ShopRepository.GetAll().OrderByDescending(a => a.ShopCord).FirstOrDefault();
            if(paths==null)
            {
                request.Data.ShopCord = "DYS-001";
            }
            else
            {
                var str =Convert.ToInt32( paths.ShopCord.Substring(4))+1;
                request.Data.ShopCord = "DYS-" + str.ToString().PadLeft(3, '0');
            }

            request.Data.IsDel = false;
            request.Data.IsShop = false;
            request.Data.IsMoney = true;
            request.Data.IsGo = false;
           var path= await ShopRepository.AddAsync(request.Data);
            return new APIResult<int>
            {
                Code = ResultCode.success,
                Mag = "",
                data = path
            };


        }
    }
}
